package com.sanpu.kxe.business.repay.model;

import com.alibaba.fastjson.JSONObject;
import com.sanpu.kxe.business.repay.bean.AddRepayOutput;
import com.sanpu.kxe.business.repay.bean.AreaOutput;
import com.sanpu.kxe.business.repay.bean.MccListOutput;
import com.sanpu.kxe.business.repay.bean.QueryPlanDetailsOutput;
import com.sanpu.kxe.business.repay.bean.QueryPlanOutput;
import com.sanpu.kxe.business.repay.bean.RateDetailsOutput;
import com.sanpu.kxe.business.repay.bean.RefundPlanInput;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.bean.RepayCreditOutput;
import com.sanpu.kxe.rxservice.client.RepayClient;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;

/**
 * Description：代还模块model
 * Created by 薛勇军 on 2019/4/10.
 */

public class RepayModel {

    private static volatile RepayModel mInstance;

    private RepayModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static RepayModel getInstance() {
        RepayModel result = mInstance;
        if (result == null) {
            synchronized (RepayModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new RepayModel();
                }
            }
        }
        return result;
    }

    /**
     * 代还首页
     */
    public Observable<RepayCreditOutput> index(String apprtId) {
        JSONObject reqData = new JSONObject();
        reqData.put("apprtId", apprtId);
        return RepayClient.getInstance().index(reqData);
    }

    /**
     * 解绑信用卡
     */
    public Observable<String> unbindCard(String cardId) {
        JSONObject reqData = new JSONObject();
        reqData.put("cardId", cardId);
        return RepayClient.getInstance().unbindCard(reqData);
    }

    /**
     * 绑定信用卡
     */
    public Observable<AddRepayOutput> bindCard(String bankId, String apprtId, String billDay, String dueDay) {
        JSONObject reqData = new JSONObject();
        reqData.put("bankId", bankId);
        reqData.put("apprtId", apprtId);
        reqData.put("billDay", billDay);
        reqData.put("dueDay", dueDay);
        return RepayClient.getInstance().bindCard(reqData);
    }

    /**
     * 获取费率列表
     */
    public Observable<List<RateDetailsOutput>> repayFeeList() {
        return RepayClient.getInstance().repayFeeList();
    }

    /**
     * 卡片计划列表
     */
    public Observable<List<QueryPlanOutput>> cardPlanList(String bindCardId) {
        JSONObject reqData = new JSONObject();
        reqData.put("bindCardId", bindCardId);
        return RepayClient.getInstance().cardPlanList(reqData);
    }

    /**
     * 通过状态查询卡片计划列表
     * @param planStatus ALL：全部
     *                   PROCESING：执行中
     *                   SUCCESS：执行成功
     *                   FAIL：执行失败
     *                   COMPENSATE：补偿
     */
    public Observable<List<QueryPlanOutput>> cardPlanListForStatus(int nowPage, String mercCardId, String planStatus) {
        JSONObject reqData = new JSONObject();
        reqData.put("nowPage", nowPage);
        reqData.put("mercCardId", mercCardId);
        reqData.put("planStatus", planStatus);
        return RepayClient.getInstance().cardPlanListForStatus(reqData);
    }

    /**
     * 卡片计划明细列表
     */
    public Observable<QueryPlanDetailsOutput> cardPlanDetailList(String repayOrderNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("repayOrderNo", repayOrderNo);
        return RepayClient.getInstance().cardPlanDetailList(reqData);
    }

    /**
     * 取消计划
     */
    public Observable<String> cancelPlan(String repayOrderNo) {
        JSONObject reqData = new JSONObject();
        reqData.put("repayOrderNo", repayOrderNo);
        return RepayClient.getInstance().cancelPlan(reqData);
    }

    /**
     * 生成计划
     */
    public Observable<RefundPlanOutput> createPlan(RefundPlanInput refundPlanInput) {
        JSONObject reqData = new JSONObject();
        if (refundPlanInput != null) {
            reqData = JSONObject.parseObject(JSONObject.toJSON(refundPlanInput).toString());
        }
        return RepayClient.getInstance().createPlan(reqData);
    }

    /**
     * 修改计划还款天数
     */
    public Observable<RefundPlanOutput> updatePlanPaymentDays(String repayOrderNo, String expectedPaymentDays) {
        JSONObject reqData = new JSONObject();
        reqData.put("repayOrderNo", repayOrderNo);
        reqData.put("expectedPaymentDays", expectedPaymentDays);
        return RepayClient.getInstance().updatePlanPaymentDays(reqData);
    }

    /**
     * 获取行业列表
     */
    public Observable<List<MccListOutput>> getMccList() {
        return RepayClient.getInstance().getMccList();
    }

    /**
     * 执行计划
     */
    public Observable<String> executePlan(String apprtId, String repayOrderNo, String provCode, String cityCode,
                                          List<RefundPlanOutput.RefundPlan> refundPlanList) {
        JSONObject reqData = new JSONObject();
        reqData.put("apprtId", apprtId);
        reqData.put("repayOrderNo", repayOrderNo);
        reqData.put("provCode", provCode);
        reqData.put("cityCode", cityCode);
        reqData.put("detailList", refundPlanList);
        return RepayClient.getInstance().executePlan(reqData);
    }

    /**
     * 修改卡信息
     */
    public Observable<String> updateCard(String cardId, String rsdTel, String billDay, String dueDay) {
        JSONObject reqData = new JSONObject();
        reqData.put("cardId", cardId);
        reqData.put("rsdTel", rsdTel);
        reqData.put("billDay", billDay);
        reqData.put("dueDay", dueDay);
        return RepayClient.getInstance().updateCard(reqData);
    }

    /**
     * 修改卡信息
     */
    public Observable<String> updateCreditBank(String merchantCode, String bankNo, String rsdTel,
                                               String billDay, String dueDay) {
        JSONObject reqData = new JSONObject();
        reqData.put("merchantCode", merchantCode);
        reqData.put("bankNo", bankNo);
        reqData.put("rsdTel", rsdTel);
        reqData.put("billDay", billDay);
        reqData.put("dueDay", dueDay);
        return RepayClient.getInstance().updateCreditBank(reqData);
    }

    /**
     * 获取省市区
     */
    public Observable<AreaOutput> provinces() {
        return RepayClient.getInstance().provinces();
    }

    /**
     * 代还日期选择
     */
    public List<String> getSelectList() {
        List<String> selectList = new ArrayList<>();
        for (int i = 0; i < 31; i++) {
            selectList.add("每月" + (i + 1) + "号");
        }
        return selectList;
    }
}
